package cn.neusoft.dao.impl;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Food;
import cn.neusoft.dao.IFoodDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDao implements IFoodDao {
    private QueryRunner queryRunner;

    public FoodDao() {
        queryRunner = JdbcUtils.getQueryRunner();
    }

    @Override
    public int add(Food food) throws SQLException {
        String sql = "INSERT INTO food(foodName,foodExplain,foodImg,foodPrice,businessId,remarks) VALUES(?,?,?,?,?,?)";
        return queryRunner.update(sql,
                food.getFoodName(),
                food.getFoodExplain(),
                food.getFoodImg(),
                food.getFoodPrice(),
                food.getBusinessId(),
                food.getRemarks()
                );
    }

    @Override
    public int update(Food food) throws SQLException {
        String sql = "UPDATE food SET foodName=?,foodExplain=?,foodImg=?,foodPrice=?,businessId=?,remarks=? WHERE foodId=?";
        return queryRunner.update(sql,
                food.getFoodName(),
                food.getFoodExplain(),
                food.getFoodImg(),
                food.getFoodPrice(),
                food.getBusinessId(),
                food.getRemarks(),
                food.getFoodId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from food where foodId=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public Food getById(Integer id) throws SQLException {
        String sql = "SELECT foodId,foodName,foodExplain,foodImg,foodPrice,businessId,remarks FROM food WHERE foodId=?";
        return queryRunner.query(sql,new BeanHandler<Food>(Food.class),id);
    }

    @Override
    public List<Food> getAll() throws SQLException {
        String sql = "SELECT foodId,foodName,foodExplain,foodImg,foodPrice,businessId,remarks FROM food";
        return queryRunner.query(sql, new BeanListHandler<Food>(Food.class));
    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        //传递的真实类型是BusinessQuery  所以强转  目的是获取搜索条件
        FoodQuery query = (FoodQuery) baseQuery;
        //填充?的参数集合  因为sql是动态拼接的  所以?的数量不确定 有需要就加入到集合  不需要则不加入
        List<Object> params = new ArrayList<>();
        String sql = "select count(foodId) from food where 1=1 ";
        if(query.getFoodName()!=null) {
            sql += " and foodName like concat('%',?,'%')";
            params.add(query.getFoodName());
        }
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray()).intValue();
    }

    @Override
    public List<Food> getByPage(BaseQuery baseQuery) throws SQLException {
        //传递的真实类型是FoodQuery  所以强转  目的是获取搜索条件
        FoodQuery query = (FoodQuery) baseQuery;
        //填充?的参数集合  因为sql是动态拼接的  所以?的数量不确定 有需要就加入到集合  不需要则不加入
        List<Object> params = new ArrayList<>();
        //可以动态拼接条件搜索的部分  where 1=1
        String sql = "SELECT foodId,foodName,foodExplain,foodImg,foodPrice,businessId,remarks FROM food" +
                " where 1=1";
        //动态sql  根据条件参数的传递来确定要不要拼接sql
        if(query.getFoodName()!=null) {
            sql += " and foodName like concat('%',?,'%')";
            params.add(query.getFoodName());
        }
        sql += " ORDER BY foodId DESC limit ?,?";
        //给limit后面的两个?填充的  一直都有
        params.add(query.getStartIndex());
        params.add(query.getPageCount());

        System.out.println("查询集合："+sql);
        System.out.println("参数集合："+params);

        return queryRunner.query(sql, new BeanListHandler<Food>(Food.class), params.toArray());
    }
}
